//
//  SDHSportsTabbar.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/4.
//

#import "SDHSportsTabbar.h"
#import "SDHSportsTabBarVC.h"

@interface SDHSportsTabbar ()

@property (nonatomic, assign) CGFloat bumpHeight;
@property (nonatomic, strong) NSMutableArray *arrTabBarButton;
@property (nonatomic, copy) DidTapBumpBarBlock didTapBumpBarBlock;

@end

@implementation SDHSportsTabbar

- (instancetype)initWithBumpIndex:(NSInteger)index bumpHeight:(CGFloat)bumpHeight didTapBumpBarBlock:(DidTapBumpBarBlock)didTapBumpBarBlock{
    if (self = [super init]) {
        _index = index;
        _bumpHeight = bumpHeight;//凸起的高度
        _didTapBumpBarBlock = didTapBumpBarBlock;
        _arrTabBarButton = [NSMutableArray array];
        
        self.backgroundImage = [UIImage imageFromColor:[UIColor clearColor]];
        [self setShadowImage:[UIImage imageFromColor:[UIColor clearColor] size:CGSizeMake(kScreenWidth, 0.5)]];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_arrTabBarButton removeAllObjects]; // 防止重复添加
    for (id obj in self.subviews) {
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            //一个可变数组保存所有的UITabBarButton按钮
            [_arrTabBarButton addObject:obj];
        }
    }
    
    // 确保tab按钮，按照x位置，有序排列的
    [_arrTabBarButton sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        UIView *v1 = (UIView *)obj1;
        UIView *v2 = (UIView *)obj2;
        if (v1.frame.origin.x > v2.frame.origin.x) {
            return YES;
        }
        return NO;
    }];
    
    /** 循环防止重复创建  也可以把roundView设置成全局的 把初始化方法写在UITabbar 的init方法里就不需要这个循环了 */
    for (id obj in self.subviews) {
        if ([obj isKindOfClass:[UIControl class]]) {
            UIControl *view = obj;
            if (view.tag == 123456) {
                [view removeFromSuperview];
            }
        }
    }
    //_index是传进来的需要凸起的index
    UIButton *button = _arrTabBarButton[_index];
    UIControl *roundView = [[UIControl alloc]initWithFrame:CGRectMake(button.frame.origin.x, -_bumpHeight, button.frame.size.width, button.frame.size.width)];
    roundView.tag = 123456;
    roundView.layer.cornerRadius = roundView.frame.size.height*0.5;
    roundView.clipsToBounds = YES;
    roundView.backgroundColor = [UIColor whiteColor];
    [roundView addTarget:self action:@selector(tapControl:) forControlEvents:UIControlEventTouchUpInside];
    [self insertSubview:roundView atIndex:0];
}

//凸起部分的点击事件
- (void)tapControl:(UIControl *)control{
    if (_didTapBumpBarBlock) {
        _didTapBumpBarBlock(_index);
    }
}
//由于凸起部分在UITabbar frame外 使用该方法解决不能点击到
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    //如果不加上这个判断，当push到其他页面点击这个位置也会响应
    if (!self.hidden) {
        UIControl *roundView = [self viewWithTag:123456];
        if (CGRectContainsPoint(roundView.frame, point)) {
            return roundView;
        }
    }
    return [super hitTest:point withEvent:event];
}

@end
